Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 9 - Script Objects


Sending Commands to Script Objects

You use Tell statements to send commands to script objects. A Tell statement sent to a script object is similar to a Tell statement sent to an application, except that it uses a variable name, instead of a reference, to identify the script object. For example,

tell John
   sayHello to "Herb"   sayHello to "Grace"end tell
sends two sayHello commands to the script object John. The parameters of the commands in the Tell statement, if any, must match the parameters defined in the handler definitions in the script object definition. For example, the statement

tell John
   sayHello ("Herb")
end tell
--results in an error
results in an error message because the handler definition for the sayHello command (shown earlier in this chapter) defines a labeled parameter, not a positional parameter.

For a script object to respond to a command within a Tell statement, either the script object or its parent script object must have a handler for the command. A parent script object is a script object from which a script object inherits handlers and properties. (For more information about parent script objects, see "Inheritance and Delegation" on page 271.)

The one command that any script object can handle, even without an explicitly defined handler, is the Run command. A handler for the Run command can consist of all statements at the top level of a script object definition other than property and handler definitions. If the script object definition contains only

handler and property definitions, and does not include any additional top-level statements, the definition may include an explicit Run handler that begins with on run. If a script object definition includes neither an implicit Run handler (in the form of top-level statements) nor an explicit Run handler, the Run command doesn't do anything. (For more information about Run handlers, see "Command Handlers for Script Applications," which begins on page 243.)

For example, the Display Dialog command in the following script object definition is executed only if you send a Run command to script object John.

script John
   property HowManyTimes : 0
   to sayHello to someone
      set HowManyTimes to HowManyTimes + 1
      return "Hello " & someone
   end sayHello
   display dialog "John received the Run command"end script

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996